<?php
/**
 * 
 * @author Youssef Driouach
 *
 */
class Application_Model_AddressMapper{
	
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Address');
		}
		return $this->_dbTable;
	}
	
	
	public function save(Application_Model_Address $address)
	{
		$data = array(
				'text'   		=> $address->getText(),
				'zipCode' 		=> $address->getZipCode(),
				'city' 			=> $address->getCity(),
				'country' 		=> $address->getCountry(),
				'latitude'		=> $address->getLatitude(),
				'longitude'		=> $address->getLongitude(),
				'admin_area_lvl1'	=> $address->getAdmin_area_lvl1(),
				'admin_area_lvl2'	=> $address->getAdmin_area_lvl2(),
		);
	
		
		$where = array(
					'text = ?' 		=> $address->getText(),
				 	'zipCode = ?' 		=> $address->getZipCode(), 
					'city = ?' 			=>  $address->getCity(), 
					'country = ?' 		=>  $address->getCountry(),
					'latitude = ?'			=> $address->getLatitude(),
					'longitude = ?'			=> $address->getLongitude(),
					'admin_area_lvl1 = ?'	=> $address->getAdmin_area_lvl1(),
					'admin_area_lvl2 = ?'	=> $address->getAdmin_area_lvl2(),
				);
		//$id = $this->getDbTable()->update($data, $where);
// 		$select = $this->getDbTable()->select()->where('text = ?', $address->getText())
// 												->where('zipCode = ?' , $address->getZipCode())
// 												->where('city = ?' ,  $address->getCity())
// 												->where('country = ?' ,  $address->getCountry());
		$row = new Zend_Db_Table_Row();
		$row = $this->getDbTable()->fetchRow($where);
		if($row == null)
		{
			unset($data['id_address']);
			return $this->getDbTable()->insert($data);
		}else{
			$this->getDbTable()->update($data, $where);
			return $row->save();
		}
	}
	
	public function find($id, Application_Model_Address $address)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$address->setAddressId($row->id_address);
		$address->setText($row->text)
		->setZipCode($row->zipCode)
		->setCity($row->city)
		->setCountry($row->country);
		$address->setLatitude($row->latitude);
		$address->setLongitude($row->longitude);
		$address->setAdmin_area_lvl1($row->admin_area_lvl1);
		$address->setAdmin_area_lvl2($row->admin_area_lvl2);
		return $address;
	}
	
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Address();
			$entry->setId($row->id_address)
			->setText($row->text)
			->setZipCode($row->zipCode)
			->setCity($row->city)
			->setCountry($row->country)
			->setLatitude($row->latitude)
			->setLongitude($row->longitude)
			->setAdmin_area_lvl1($row->admin_area_lvl1)
			->setAdmin_area_lvl2($row->admin_area_lvl2);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function delete($id, Application_Model_Address $address)
	{
		$result = $this->getDbTable()->find($id); 
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_address = ?' => $id));
		}
		
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Address $address)
	{
		
		$department = $address->getAdmin_area_lvl2();
		$region = $address->getAdmin_area_lvl1();
		$country = $address->getCountry();
		$doc->addField(Zend_Search_Lucene_Field::Text('admin_area_lvl2', $department, 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('admin_area_lvl1', $region, 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('country', $country, 'utf-8'));
		return $doc;
	}
}